<?php

class MD_Event extends ModelBase {

    function MD_Event(&$database) {
	$this->tableName = TABLE_EVENT;
	$this->keyField = "id";
	ModelBase::ModelBase($database);
    }

    function getById($id) {
	$sql = "SELECT *  FROM $this->tableName ";
	if ($id != "") {
	    $sql .= " WHERE $this->keyField =$id ";
	}
	return $this->db->retrieve($sql);
    }

    function get_events_type_by_Where($where, $order, $limit) {
	$sql = "SELECT *  FROM tbl_events_type";
	if ($where != "") {
	    $sql .= " WHERE $where ";
	}
	if ($order != "") {
	    $sql .= " ORDER BY $order ";
	}
	if ($limit != "") {
	    $sql .= " LIMIT $limit ";
	}
//	echo $sql."<br/>";
	return $this->db->retrieve($sql);
    }

    function get_events_type_by_Where_1($where, $order, $limit) {
	$sql = "SELECT *  FROM tbl_events_type";
	if ($where != "") {
	    $sql .= " WHERE $where ";
	}
	if ($order != "") {
	    $sql .= " ORDER BY $order ";
	}
	if ($limit != "") {
	    $sql .= " LIMIT $limit ";
	}
//	echo $sql."<br/>";
	return $this->db->getFirstRecord($sql);
    }

    function insert_type(&$arrayData) {
	$listFilds .= "event_type_name";
	$listValues .= "'" . $this->replaceString(&$arrayData["event_type_name"]) . "'";
	$listFilds .= ",event_type_note";
	$listValues .= ",'" . $this->replaceString(&$arrayData["event_type_note"]) . "'";
	$listFilds .= ",parent_id";
	$listValues .= ",'" . $this->replaceString(&$arrayData["parent_id"]) . "'";

	$sql = "INSERT INTO tbl_events_type (  $listFilds ) ";
	$sql .= " VALUES ($listValues ); ";
//	echo $sql;
//	die();
	$this->db->query($sql);
    }

    function delete_type($id) {
	$sql = "DELETE FROM tbl_events_type WHERE $this->keyField =$id";
//	echo $sql;
	return $this->db->query($sql);
    }

    function update_type(&$arrayData) {
	$sql = "UPDATE  tbl_events_type SET ";
	$sql .= "event_type_name='" . $this->replaceString(&$arrayData["event_type_name"]) . "'";
	$sql .= ",event_type_note='" . $this->replaceString(&$arrayData["event_type_note"]) . "'";
	$sql .= ",parent_id='" . $this->replaceString(&$arrayData["parent_id"]) . "'";

	$sql .= " WHERE $this->keyField=" . $arrayData["type_id"];
//	echo $sql;
//	die();
	$this->db->query($sql);
    }

    function delete_events($id) {
	$sql = "DELETE FROM $this->tableName WHERE $this->keyField =$id";
//	echo $sql;
	return $this->db->query($sql);
    }

    function update_events(&$arrayData) {
	$sql = "UPDATE  " . $this->tableName . " SET ";
	$sql .= "title_event='" . $this->replaceString(&$arrayData["title_event"]) . "'";
	$sql .= ",author_event='" . $this->replaceString(&$arrayData["author_event"]) . "'";
	$date_event = date('Y-m-d', strtotime(&$arrayData["date_event"]));
	$sql .= ",date_event='" . $this->replaceString($date_event) . "'";
	$sql .= ",content_event='" . $this->replaceString(&$arrayData["content_event"]) . "'";
	$sql .= ",header_event='" . $this->replaceString(&$arrayData["header_event"]) . "'";
	$sql .= ",event_type='" . $this->replaceString(&$arrayData["event_type"]) . "'";
	$sql .= ",show_home='" . $this->replaceString(&$arrayData["show_home"]) . "'";
	if ($_FILES["image_event"]['name'] != "") {
	    $sql .= ",image_event='" . $_SESSION['Pkd_images_ALL'] . "'";
	}
	$sql .= " WHERE $this->keyField=" . $arrayData["id"];
//	echo $sql;die();
	$this->db->query($sql);
    }

    function insert_events(&$arrayData) {
	$listFilds .= "title_event";
	$listValues .= "'" . $this->replaceString(&$arrayData["title_event"]) . "'";
	$listFilds .= ",author_event";
	$listValues .= ",'" . $this->replaceString(&$arrayData["author_event"]) . "'";
	$date_event = date('Y-m-d', strtotime(&$arrayData["date_event"]));
	$listFilds .= ",date_event";
	$listValues .= ",'" . $this->replaceString($date_event) . "'";
	$listFilds .= ",content_event";
	$listValues .= ",'" . $this->replaceString(&$arrayData["content_event"]) . "'";
	$listFilds .= ",header_event";
	$listValues .= ",'" . $this->replaceString(&$arrayData["header_event"]) . "'";
	$listFilds .= ",event_type";
	$listValues .= ",'" . $this->replaceString(&$arrayData["event_type"]) . "'";
	$listFilds .= ",show_home";
	$listValues .= ",'" . $this->replaceString(&$arrayData["show_home"]) . "'";

	if ($_FILES["image_event"]['name'] != "") {
	    $listFilds .= ",image_event";
	    $listValues .= ",'" . $_SESSION['Pkd_images_ALL'] . "'";
	}

	$sql = "INSERT INTO $this->tableName (  $listFilds ) ";
	$sql .= " VALUES ($listValues ); ";
//	echo $sql;
//	die();
	$this->db->query($sql);
    }

}

?>